package com.exoweb.common.business {
    import mx.rpc.events.FaultEvent;
    import mx.rpc.events.ResultEvent;

    import com.exoweb.common.business.GameService;
    import com.exoweb.common.PopUpMessageManager;
    import com.exoweb.common.CasualGameConstants;

    public class CasualGameService extends GameService {
        public var hasScore:Boolean = true;

        public function CasualGameService() {
            // A super statement cannot occur after a this, super, return, or throw statement
            super();
        }

        public function startGame(): void {
            this.addEventListeners(onStartGameSuccess, onStartGameFault);
            this.remoteObject.startGame({"user_id": userId,
                                         "auth_info": authinfo},
                                        {"game_id": gameId,
                                         "game_setting_id": gameSettingId,
                                         "level": model.gameVo.level})
        }

        public function resultGame():void {
            this.addEventListeners(onResultGameSuccess, onResultGameFault);
            var gameInfo:Object = {"game_id": gameId,
                                   "game_setting_id": gameSettingId,
                                   "game_record_id": model.gameVo.gameRecordId,
                                   "eggs": model.gameVo.eggs,
                                   "level": model.gameVo.level};
            if (this.hasScore) {
                gameInfo.score = model.gameVo.score;
            }
            this.remoteObject.resultGame({"user_id": userId,
                                          "auth_info": authinfo},
                                         gameInfo);
        }

        private function onStartGameSuccess(result:ResultEvent):void {
            this.removeEventListeners(onStartGameSuccess, onStartGameFault);
            this.model.userVo.balance = result.message.body.newBalance;
            this.model.gameVo.gameRecordId = result.message.body.gameRecordId;
            this.model.gameVo.state = CasualGameConstants.GAME_STATE.RUNNING;
        }

        private function onStartGameFault(fault:FaultEvent):void {
            this.removeEventListeners(onStartGameSuccess, onStartGameFault);
            this.em.data = checkFaultMessage(fault);
            PopUpMessageManager.popUpUnique(this.em);
        }

        private function onResultGameSuccess(result:ResultEvent):void {
            this.removeEventListeners(onResultGameSuccess, onResultGameFault);
            if (result.message.body.newBalance) {
                this.model.gameVo.newBalance = result.message.body.newBalance;
            }
            this.model.gameVo.eggResult = result.message.body.eggResult;
            this.model.gameVo.jackpotsWinners = result.message.body.jackpotsWinners;
            this.model.gameVo.globalJackpotsWinners = result.message.body.globalJackpotsWinners;
            this.model.gameVo.state = CasualGameConstants.GAME_STATE.SHOW_EGGS;
        }

        private function onResultGameFault(fault:FaultEvent):void {
            this.removeEventListeners(onResultGameSuccess, onResultGameFault);
            this.em.data = checkFaultMessage(fault);
            PopUpMessageManager.popUpUnique(this.em);
            this.model.gameVo.state = CasualGameConstants.GAME_STATE.PREPARING;
        }
    }
}

